package controlSoftware.util;

import java.io.File;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Common {

    public final static String xml = "xml";
    public final static String txt = "txt";
    
    /**
     * Get the extension of a file.
     */  
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }

    
    
    /**
	 * takes a xml element and the tag name, look for the tag and get
	 * the integer content
	 */
	public static int getIntValue(Element nNode, String tag) {	
		int num = -1;
		
		if (getTextValue(nNode,tag)!=null)
			num = Integer.parseInt(getTextValue(nNode,tag));
		
		return num;

	}

	/**
	 * takes a xml element and the tag name, look for the tag and get
	 * the text content
	 */
	public static String getTextValue(Element nNode, String tag) {
		String text = "";
		NodeList elements = nNode.getElementsByTagName(tag);

		if(elements != null && elements.getLength() > 0) {
			Element el = (Element)elements.item(0);
			if (el.getFirstChild()!= null)
				text = el.getFirstChild().getNodeValue();
		}

		return text;
	}
}
